home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 / IRIX 6.2 CD2.iso / dist / print.idb / usr / lib / print / submitjob.z / submitjob
Text File  |  1996-06-10  |  7KB  |  266 lines

  1. #!/bin/sh
  2. #
  3. # REQUIRES: At least two arguments:
  4. #    printername:    the installed name of the printer 
  5. #            to send the print jobs to.
  6. #    printjobs:    the full path to the files to print.
  7. #
  8. # MODIFIES: Tag of printer active icon file and the contents
  9. #    of /var/spool/lp/logfile if VERBOSE=1.
  10. #
  11. # EFFECTS:
  12. # This is a program which invokes routeprint to submit the jobs
  13. # indicated on the command line.  It also updates the status bits
  14. # on the printer interface file tagline, in order to provide better
  15. # user feedback on the status of the printer.
  16. #
  17. # This program is intended to be used in conjunction with the Indigo
  18. # Magic dsktop.
  19. #
  20. # COMMENTS:
  21. #
  22. # Printer type is a system-wide defined token, e.g., ColorPostScript.
  23. # Valid printer tags range from 0x10200-0x102FF, 66048-66287.
  24. # This program does not manipulate the base tags, only the status bits.
  25. # Only tagit and the ftr rules care what the base tag #s are.
  26. #
  27.  
  28. # Send errors to the spooler logfile, defined below
  29. LOGFILE=/var/spool/lp/log
  30.  
  31. # Whether verbose messages get logged to logfile, true if VERBOSE > 0.
  32. VERBOSE=0
  33.  
  34. # Where printers live:
  35. #
  36. PRINTERPATH=/var/spool/lp/interface
  37.  
  38. # Tagging program:
  39. #
  40. TAGPROGRAM=/usr/lib/print/tagprinter
  41.  
  42. #
  43. # Some defines to make tagging easier:
  44. #
  45. # The range of valid printer tags:
  46. #
  47. MINTAG=66048           # 0x10200
  48. MAXTAG=66287           # 0x102FF
  49. #
  50. # The state modifiers for those type tags:
  51. #
  52. IDLE=0               # 0x0
  53. BUSY=1               # 0x1
  54. FAULTED=2           # 0x2
  55. UNKNOWN=3           # 0x3
  56. UNAVAILABLE=3           # 0x3
  57.  
  58.  
  59. #########################################################################
  60. #
  61. #    CONVENIENCE FUNCTIONS
  62. #
  63. #########################################################################
  64.  
  65.  
  66. TagPrinter()
  67. #
  68. # REQUIRES: two arguments:     printername to tag, 
  69. #                    desired printer state (idle or busy)
  70. #
  71. # MODIFIES: Active icon file if conditions are appropriate.
  72. #
  73. # RETURNS: 0 if successful, non-0 if any segment failed.
  74. #
  75. # EFFECTS:
  76. #
  77. # This routine attempts to mark the printer busy.
  78. # It will only tag the printer as busy if it is currently idle.
  79. # This ensures that a faulted or unavailable status is not overridden,
  80. # and that a busy status is not needlessly rewritten.
  81. #
  82. # The routine will return with an error status if the printer is not tagged.  
  83. # The printer will not be tagged if it is not already tagged.
  84. #
  85. {
  86.  
  87.     # Grab command line arguments
  88.     #
  89.     target=$1         # the file to tag
  90.     targetstate=$2         # the desired state to tag the printer
  91.     
  92.     # Set up our destination status bits:
  93.     #
  94.     case $targetstate in
  95.       idle|Idle|IDLE)   DESTSTATUS=$IDLE      ;;
  96.       busy|Busy|BUSY)   DESTSTATUS=$BUSY       ;;
  97.       *)
  98.          su lp -c "echo $myname: ERROR: Invalid destination state $targetstate requested. >> $LOGFILE"
  99.          return 1
  100.          ;;
  101.     esac
  102.  
  103.     # OK, we've done all the setup, let's do the real work:
  104.     #
  105.     
  106.     # ALGORITHM:
  107.     #
  108.     # First, grab the tag, checking to make sure it's valid.
  109.     # Next, strip off the status bits (low-order bits).
  110.     # If the status bits say the printer is IDLE and we're to tag BUSY,
  111.     #   retag the printer as BUSY, and return w/exit code of tag command.
  112.     # If the status bits say the printer is BUSY and we're to tag IDLE,
  113.     #   retag the printer as IDLE, and return w/exit code of tag command.
  114.     # If the status bits say any other status, do nothing and return.
  115.     # If the status bits are invalid, report a warning and return.
  116.     #
  117.     # Done.
  118.     #
  119.  
  120.     tag=`$TAGPROGRAM $target`
  121.  
  122.     if [ $VERBOSE != 0 ]; then
  123.        su lp -c "echo Found tag $tag on printer $target >> $LOGFILE"
  124.     fi
  125.  
  126.     if [ "$tag" -lt "$MINTAG" -o "$tag" -ge "$MAXTAG" ] ;
  127.     then
  128.       su lp -c "echo $myname: Supposed printer $target is not tagged as a valid printer. >> $LOGFILE"
  129.       return 1
  130.     fi
  131.     
  132.     STATUS=`expr $tag % 4`
  133.     
  134.     TYPE=`expr $tag - $STATUS`
  135.     
  136.     case $STATUS in
  137.       $IDLE)                
  138.          # This is the only case in which we modify the target file.
  139.          # We mark it busy iff it is idle and we're to mark busy.
  140.          # 
  141.          if [ $DESTSTATUS = $BUSY ];
  142.          then
  143.               if [ $VERBOSE != 0 ];
  144.           then
  145.               su lp -c "echo $myname: Tagging printer $target as BUSY. >> $LOGFILE"
  146.           fi
  147.          
  148.               su lp -c "$TAGPROGRAM $target `expr $TYPE + $BUSY`"
  149.     
  150.           # Finally, return same status code as the tag command
  151.           #    
  152.           return $?
  153.          fi
  154.          ;;
  155.     
  156.       $BUSY)
  157.          if [ $DESTSTATUS = $IDLE ];
  158.          then
  159.               if [ $VERBOSE != 0 ];
  160.           then
  161.               su lp -c "echo $myname: Tagging printer $target as IDLE. >> $LOGFILE"
  162.           fi
  163.          
  164.               su lp -c "$TAGPROGRAM $target `expr $TYPE + $IDLE`"
  165.     
  166.           # Finally, return same status code as the tag command
  167.           #    
  168.           return $?
  169.          fi
  170.          ;;    
  171.     
  172.       # Do nothing to other status cases: we don't touch these valid types.
  173.       # 
  174.       $FAULTED|$UNKNOWN|$UNAVAILABLE)     
  175.          if [ $VERBOSE != 0 ];
  176.          then
  177.         su lp -c "echo $myname: Printer $target in faulted state, not tagging. >> $LOGFILE"
  178.          fi
  179.          ;;
  180.     
  181.       # Echo an error if the type is invalid.
  182.       #
  183.       *)    
  184.         if [ $VERBOSE -ne 0 ];
  185.         then
  186.             su lp -c "echo $myname: WARNING: Invalid status read from printer $target: $STATUS >> $LOGFILE"
  187.         fi
  188.         ;;
  189.     esac
  190.     
  191.     # If we get here, no error occurred and we didn't retag anything.
  192.     # return successfully
  193.     #
  194.     return 0 
  195. }
  196.  
  197.  
  198. #
  199. # Check command line arguments
  200. #
  201. myname=`basename $0`
  202.  
  203. # Check number of command line arguments
  204. #
  205. if [ $# -lt 2 ]   # if not proper number of arguments,
  206. then           # exit with an error
  207.    su lp -c "echo $myname: ERROR: No files specified in print request. >> $LOGFILE"
  208.    su lp -c "echo $myname: Usage: $myname PrinterName FilesToPrint >> $LOGFILE"
  209.    exit 1
  210. fi
  211.  
  212. # Grab command line arguments
  213. #
  214. printername=$1         # the file to tag
  215. shift 1            # get rid of first args
  216. filestoprint="$*"       # grab rest of cmd line args
  217.  
  218. # Check existence of target printer
  219. #
  220. if [ ! -s $PRINTERPATH/$target ]     # if file doesn't exist
  221. then
  222.     su lp -c "echo $myname: ERROR: Target printer $target does not exist! >> $LOGFILE"
  223.     return 1
  224. fi
  225.     
  226. # Mark the printer busy iff it is idle
  227. #
  228. TagPrinter $printername busy
  229.  
  230. # Check whether TagPrinter failed.
  231. #
  232. if [ $? -ne 0 -a $VERBOSE != 0 ];
  233. then # something went wrong, report an error.
  234.      su lp -c "echo $myname: Error while tagging printer. >> $LOGFILE"
  235. fi    
  236.  
  237. # Submit the jobs as best we can
  238. #
  239. routeprint -g -p $printername $filestoprint
  240.  
  241. # Catch the exit status of routeprint: did it work?
  242. #
  243. exitstatus=$?
  244.  
  245. # If something failed, "unbusy" the printer and exit.
  246. #
  247. if [ $exitstatus -ne 0 ] ; 
  248. then # something went wrong
  249.     if [ $VERBOSE != 0 ];
  250.     then # something went wrong, report an error.
  251.         su lp -c "echo $myname: Error in routeprint, tagging printer idle. >> $LOGFILE"
  252.     fi        
  253.  
  254.     TagPrinter $printername idle ; 
  255.  
  256.     # Check whether TagPrinter failed.
  257.     #
  258.     if [ $? -ne 0 -a $VERBOSE != 0 ];
  259.     then # something went wrong, report an error.
  260.         su lp -c "echo $myname: Error while tagging printer. >> $LOGFILE"
  261.     fi    
  262. fi                  
  263.  
  264. exit $exitstatus
  265.  
  266.